import { Module } from '@nestjs/common';
import { ServeStaticModule } from '@nestjs/serve-static';
import { ConfigModule, ConfigService, ConfigType } from '@nestjs/config';

import appConfig from '@config/app.config';
import resourceConfig from '@config/resource.config';

@Module({
  imports: [
    ServeStaticModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => {
        const appConfigTyped: ConfigType<typeof appConfig> | undefined =
          configService.get('app');
        const resourceConfigTyped:
          | ConfigType<typeof resourceConfig>
          | undefined = configService.get('resource');

        return [
          {
            rootPath: resourceConfigTyped!.upload.rootFilePath,
            serveRoot: `${appConfigTyped!.routeRootGroupName}${resourceConfigTyped!.staticResourcesUrl}`,
          },
        ];
      },
    }),
  ],
})
export class StaticModule {}
